package de.tum.in.eist.poll.client.gui;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

import de.tum.in.eist.poll.client.control.EIST_POLL;
import de.tum.in.eist.poll.client.control.LectureWidgetHandler;
import de.tum.in.eist.poll.shared.Lecture;
import de.tum.in.eist.poll.shared.User;

public class LectureWidgetList extends VerticalPanel implements
		AsyncCallback<ArrayList<Lecture>> {

	public static final int CURRENT_WEEK_VIEW = 0;
	public static final int LAST_WEEK_VIEW = 1;
	public static final int SEMESTER_VIEW = 2;
	public static final int NOTIFICATIONS_VIEW = 3;
	private int selectedView;

	private User user;
	private Headline headline;
	private List<Lecture> lectures;
	private Dashboard dashboard;

	public LectureWidgetList(User user, Headline headline, Dashboard dashboard) {
		this.user = user;
		this.headline = headline;
		this.dashboard = dashboard;
		// addStyleName("contentPanel");
		selectedView = LectureWidgetList.CURRENT_WEEK_VIEW;
		update();
	}

	/**
	 * remove all widgets from the list
	 */
	private void removeAll() {
		for (int i = getWidgetCount() - 1; i >= 0; i--) {
			remove(i);
		}
	}

	public void update() {
		removeAll();
		setEmptyListMessage("loading...");
		
		switch (selectedView) {
		case LectureWidgetList.CURRENT_WEEK_VIEW:
			EIST_POLL.pollSvc.requestLecturesOfCurrentWeek(user, this);
			break;
		case LectureWidgetList.LAST_WEEK_VIEW:
			EIST_POLL.pollSvc.requestLecturesOfLastWeek(user, this);
			break;
		case LectureWidgetList.SEMESTER_VIEW:
			EIST_POLL.pollSvc.requestLecturesOfCurrentSemester(user, this);
			break;
		case LectureWidgetList.NOTIFICATIONS_VIEW:
			onSuccess(user.getNotifications());
			user.clearAllNotifications();
			break;
		default:
			setEmptyListMessage(null);
			break;
		}
	}
	
	private void setEmptyListMessage(String string) {
		if (string == null)
			string = "There are no lectures in the list.";
		add(new WidgetContainer(new Label(string), false, false));
	}

	/**
	 * Select the current week as view
	 */
	public void selectCurrentWeekView() {
		selectedView = LectureWidgetList.CURRENT_WEEK_VIEW;
		update();
	}

	public void selectSemesterView() {
		selectedView = LectureWidgetList.SEMESTER_VIEW;
		update();
	}
	
	public void selectNotificationsView(){
		selectedView = LectureWidgetList.NOTIFICATIONS_VIEW;
		update();
	}

	/**
	 * Select the last two weeks as view
	 */
	public void selectLastWeekView() {
		selectedView = LectureWidgetList.LAST_WEEK_VIEW;
		update();
	}

	/**
	 * Select a special range as view
	 */
	public void selectRangeView(Date start, Date stop) {
		removeAll();
		EIST_POLL.pollSvc.requestLecturesInRange(user, start, stop, this);
	}

	@Override
	public void onFailure(Throwable caught) {
		setEmptyListMessage(caught.getMessage());
	}

	@Override
	public void onSuccess(ArrayList<Lecture> result) {
//		System.out.println(result.size() + " lectures received");
		removeAll();
		this.lectures = result;
		if (result.size() <= 0) {
			setEmptyListMessage(null);
		}
		else if (user != null) {
			for (Lecture l : result) {
				addLectureWidget(l);
			}

			// TODO Expand the first lecture in the LectureWidgetList
//			if (getWidget(0) instanceof LectureWidget) {
//				((LectureWidget)getWidget(0)).expand();
//			}
		}
		// Update Over all chart
		if (dashboard.isOverAllChartVisible()) {
			dashboard.updateOverAllChart();
		}
	}
	
	private void addLectureWidget(Lecture l) {
		LectureWidget widget;
		// Add lecture widget with notification
		if (user.getNotifications() != null
				&& user.getNotifications().contains(l.getLecturerKey())) {
			widget = new LectureWidget(user, l, true);
		}
		// Add lecture widget without notification
		else {
			widget = new LectureWidget(user, l, false);
		}

		// Add widget
		add(widget);
		new LectureWidgetHandler(widget, headline, dashboard);
	}
	
	/**
	 * Get all stats from all lectures of the current view
	 * @return
	 */
	public int[] getOverAllStats() {
		int[] result = new int[LectureVotePanel.VOTES+1];
		int[] temp;
		for (Lecture l: lectures) {
			temp = l.getStats();
			for (int i = 0; i < LectureVotePanel.VOTES; i++) {
				result[i] += temp[i];
			}
		}
		// Commit the selected view in the last field of the array
		result[LectureVotePanel.VOTES] = selectedView;
		return result;
	}

	public int getSelectedView() {
		return selectedView;
	}

	public void setSelectedView(int selectedView) {
		this.selectedView = selectedView;
	}
}
